package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.common.BaseServiceImpl;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;

import com.usian.model.common.enums.AppHttpCodeEnum;
import io.swagger.annotations.Example;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.nio.file.AccessDeniedException;
import java.util.Collection;
import java.util.Date;
import java.util.List;

//
@Service
public class AdChannelServiceImpl extends BaseServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
//        判空及判空提示
        if (dto==null) return errorResult;
//        分页信息
        Page page1=new Page(dto.page,dto.getSize());


//        条件构造器
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//       判空 模糊查询条件构造
        if (StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }

//将分页信息和查询条件作为参数传递给查询方法
        IPage result = page(page1, lambdaQueryWrapper);
//封装结果到响应结果
        //分页信息
        ResponseResult responseResult=new PageResponseResult(dto.page,dto.size,(int)result.getTotal());
        //信息数据
        responseResult.setData(result.getRecords());
return responseResult;


    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        if (null==channel)return noData;
        channel.setCreatedTime(new Date());
        save(channel);

        return Sucess;
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {
        if (adChannel==null ||adChannel.getId()==null)return errorResult;
        updateById(adChannel);
        return  Sucess;
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        if (id==null)return errorResult;
        AdChannel adChannel =(AdChannel) getById(id);
        if (adChannel==null)return noData;
        if (adChannel.getStatus())return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
        removeById(id);
        return Sucess;

    }

    @Override
    public List<AdChannel> list1() {
        List<AdChannel> list = list();
        return list;
    }
}
